package stack;

import java.util.LinkedList;

public class LinkedStack<T> {
    private LinkedList<T> stackList; //存储栈元素

    public LinkedStack() {
        this.stackList = new LinkedList<T>();
    }

    public void push(T item) {
        stackList.addFirst(item);
    }

    //出栈 并返回栈顶元素
    public T pop() {
        if (stackList.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return stackList.removeFirst();
    }
    //获取栈顶元素但是不删除

    public T peek() {
        if (stackList.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return stackList.getFirst();
    }
}
